package com.sync.utils.project.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sync.utils.common.enums.URLEnum;
import com.sync.utils.framework.okHttp.OkHttpUtils;
import com.sync.utils.common.base.service.impl.BaseServiceImpl;
import com.sync.utils.common.utils.StringUtil;
import com.sync.utils.project.demo.mapper.JokeMapper;
import com.sync.utils.project.demo.pojo.JHApiResult;
import com.sync.utils.project.demo.pojo.JokeDto;
import com.sync.utils.project.demo.service.JokeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class JokeServicesImpl extends BaseServiceImpl<JokeMapper, JokeDto> implements JokeService {

    @Autowired
    private JokeMapper jokeMapper;

    @Override
    public List<JokeDto> getJokeList() {
        Page<JokeDto> page = new Page<>(1,5);
        IPage<JokeDto> iPage = jokeMapper.selectPage(page, null);
        return iPage.getRecords();
    }

    /*@Override
    public IPage<JokeDto> getJokeIPage(HttpServletRequest request,Integer page,Integer pageSize) {
        Page<JokeDto> pageVo = new Page<>(page,pageSize);
        QueryWrapper<JokeDto> queryWrapper = new QueryWrapper();
        queryWrapper.like("content","衣服");
        IPage<JokeDto> iPage = jokeMapper.selectPage(pageVo, queryWrapper);
        long size = iPage.getSize();
        return iPage;
    }*/

    @Override
    public IPage<JokeDto> getJokeIPage(HttpServletRequest request,Integer page,Integer pageSize) {
        Page<JokeDto> pageVo = new Page<>(page,pageSize);
        QueryWrapper<JokeDto> queryWrapper = new QueryWrapper();
        //queryWrapper.like("content","衣服");
        IPage<JokeDto> iPage = jokeMapper.selectPage(pageVo, queryWrapper);
        int size = iPage.getRecords().size();
        System.out.println("size:"+size);
        return iPage;
    }

    /**
     * 循环擦取出一个列表中的所有数据
     * @return
     */
    @Override
    public int saveJokeForNet() {
        logger.info("获取response:{}",this.getRequest().getParameter("name"));
        logger.info("获取response:{}",this.getRequest().getParameter("name"));
        int page = 1;
        int pageSize = 5;
        int total = 0;
        List<JokeDto> allList = new ArrayList<>();
        while (true){
            Page<JokeDto> pageVo = new Page<>(page,pageSize,false);
            IPage<JokeDto> iPage = jokeMapper.selectPage(pageVo, null);
            List<JokeDto> list = iPage.getRecords();
            allList.addAll(list);
            int currentPageSize = iPage.getRecords().size();
            total = total+currentPageSize;
            if (currentPageSize<pageSize){
                break;
            }else{
                page++;
            }
        }
        redisCache.lSet("allList",allList);
        return total;
    }

    @Override
    public JHApiResult getJokeForNet() {
        JHApiResult jhApiResult = null;
        String rs = OkHttpUtils.builder()
                .url(URLEnum.NEWS_URL.getUrl())
                .addParam("key",URLEnum.NEWS_URL.getKey())
                .addParam("type","top")
                .post(false)
                .async();
        if (StringUtil.isNotEmpty(rs)){
            jhApiResult = JSON.parseObject(rs, JHApiResult.class);
        }
        return jhApiResult;
    }


}
